package com.sx.jys.app.framework

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.databinding.ViewDataBinding
import com.blankj.utilcode.util.KeyboardUtils
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.TitleBar
import com.sx.jys.app.framework.action.TitleBarAction
import com.sx.jys.app.until.AppActivityManager
import com.sx.jys.app.until.dismissLoadingExt
import com.sx.jys.app.until.showLoadingExt
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

/**
 * 时间　: 2019/12/21
 * 作者　: hegaojian
 * 描述　: 你项目中的Activity基类，在这里实现显示弹窗，吐司，还有加入自己的需求操作 ，如果不想用Databind，请继承
 * BaseVmActivity例如
 * abstract class BaseActivity<VM : BaseViewModel> : BaseVmActivity<VM>() {
 */
abstract class BaseActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmDbActivity<VM, DB>(),
    TitleBarAction {

    /**
     * 标题栏对象
     */
    private var mTitleBar: TitleBar? = null
    abstract override fun initView(savedInstanceState: Bundle?)
    abstract fun initData()

    /**
     * 创建liveData观察者
     */
    override fun createObserver() {}
    override fun setTitle(title: CharSequence?) {
        mTitleBar?.setTitle(title)
    }

    override fun setTitle(titleId: Int) {
        mTitleBar?.setTitle(titleId)
    }

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }
    override fun getTitleBar(): TitleBar? {
        if (mTitleBar == null) {
            mTitleBar = obtainTitleBar(getContentView())
        }
        return mTitleBar
    }

    /**
     * 和 setContentView 对应的方法
     */
    open fun getContentView(): ViewGroup? {
        return findViewById(Window.ID_ANDROID_CONTENT)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AppActivityManager.getInstance().addActivity(this.javaClass, this)
        val titleBar = titleBar
        titleBar?.setOnTitleBarListener(this)
        initData()
    }
    fun getImmersionBar(): ImmersionBar {
        return ImmersionBar.with(this).statusBarDarkFont(true)
    }

    override fun onLeftClick(v: View?) {
        super.onLeftClick(v)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        AppActivityManager.getInstance().removeActivity(this.javaClass)
    }

    open fun enableRegisterWindowInput():Boolean{
        return true
    }

    protected open fun registerWindowInput(){
        if(enableRegisterWindowInput()){
            getContentView()?.setOnTouchListener { v, event ->
                if (KeyboardUtils.isSoftInputVisible(this)) {
                    KeyboardUtils.hideSoftInput(this)
                }
                return@setOnTouchListener true
            }
        }
    }

    override fun onBackPressed() {
        KeyboardUtils.hideSoftInput(this)
        super.onBackPressed()
    }

    override fun finish() {
        KeyboardUtils.hideSoftInput(this)
        super.finish()
    }
    /* *//**
     * 在任何情况下本来适配正常的布局突然出现适配失效，适配异常等问题，只要重写 Activity 的 getResources() 方法
     *//*
    override fun getResources(): Resources {
        AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources())
        return super.getResources()
    }*/
}